import AJAX from '@/common/ajax'

export const GET_PLAY_URL = 'GET_PLAY_URL'
export const GET_PLAY_LIST = 'GET_PLAY_LIST'
export const GET_PLAYER_LIST = 'GET_PLAYER_LIST'
export const GET_PLAY_DETAIL = 'GET_PLAY_DETAIL'
export const GET_PLAYER = 'GET_PLAYER'

export default {
  namespaced: true,
  state: {
    playUrl: {},
    playList: {},
    playerList: {},
    playDetail: {},
    player: {},
    playWeek: ''
  },
  mutations: {
    [GET_PLAY_URL](state, { data, params }) {
      state.playUrl[`${params.type}_${params.url}`] = data
      console.log('state.playUrl', state.playUrl)
    },
    [GET_PLAYER](state, { data, params }) {
      state.player[`${params.id}_${params.pid}`] = data
      console.log('state.player', state.player)
    },
    [GET_PLAY_DETAIL](state, { data, params }) {
      state.playDetail[params.id] = data
      console.log('state.playDetail', state.playDetail)
    },
    [GET_PLAYER_LIST](state, { data, params }) {
      state.playerList[params.id] = data
      console.log('state.playerList', state.playerList)
    }
  },
  actions: {
    async getPlayUrl({ commit, state }, params) {
      const data = state.playUrl[`${params.type}_${params.url}`]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'getplayUrl',
          data: {
            type: params.type,
            name: params.url
          }
        })
        commit(GET_PLAY_URL, {
          data,
          params
        })
        return data
      }
    },
    async getPlayer({ commit, state }, params) {
      const data = state.player[`${params.id}_${params.pid}`]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'player',
          data: {
            id: params.id,
            pid: params.pid
          }
        })
        commit(GET_PLAYER, {
          data,
          params
        })
        return data
      }
    },
    async getDetail({ commit, state }, params) {
      const data = state.playDetail[params.id]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'detail',
          data: {
            id: params.id
          }
        })
        commit(GET_PLAY_DETAIL, {
          data,
          params
        })
        return data
      }
    },
    async getPlayerList({ commit, state }, params) {
      const data = state.playerList[params.id]
      if (data) {
        return data
      } else {
        let [err, data] = await AJAX({
          name: 'playlist',
          data: {
            id: params.id
          }
        })
        commit(GET_PLAYER_LIST, {
          data,
          params
        })
        return data
      }
    }
  }
}
